package com.huluobo.a2202mvvm_im_douyindemo.shop;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.huluobo.a2202mvvm_im_douyindemo.R;
import com.huluobo.a2202mvvm_im_douyindemo.adapter.GoodsAdapter;
import com.huluobo.a2202mvvm_im_douyindemo.bean.AddToCartBeanResult;
import com.huluobo.a2202mvvm_im_douyindemo.bean.ShopBeanResult;
import com.huluobo.a2202mvvm_im_douyindemo.cart.CartActivity;
import com.huluobo.a2202mvvm_im_douyindemo.databinding.ActivityShopBinding;
import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.BaseMVVMActivity;

import java.util.ArrayList;
import java.util.List;

public class ShopActivity extends BaseMVVMActivity<ActivityShopBinding, ShopViewModel> {

    private GoodsAdapter goodsAdapter;

    private List<ShopBeanResult.ShopBean> shopBeanList = new ArrayList<>();

    @Override
    protected void initData() {
        viewModel.getGoodsList(0, 1, 10);

        viewModel.getGoodsListSuccess.observe(this, shopBeanResult -> {
            if (shopBeanResult.getCode() == 200) {
                shopBeanList.clear();
                shopBeanList.addAll(shopBeanResult.getData());
                goodsAdapter.notifyDataSetChanged();
            } else {
                ToastUtils.showLong(shopBeanResult.getMessage());
            }
        });

        viewModel.getGoodsListFailed.observe(this, s -> {
            Log.i("TagA", "获取商品列表失败:" + s);
        });

        viewModel.addToCartSuccess.observe(this, addToCartBeanResult -> {
            ToastUtils.showLong(addToCartBeanResult.getMessage());
        });

        viewModel.addToCartFailed.observe(this, s -> {
            Log.i("TagA", "添加购物车失败:" + s);
        });
    }

    @Override
    protected void intiView() {
        dataBinding.shopRv.setLayoutManager(new GridLayoutManager(this, 2));
        goodsAdapter = new GoodsAdapter(shopBeanList);
        dataBinding.shopRv.setAdapter(goodsAdapter);

        goodsAdapter.addChildClickViewIds(R.id.item_shop_add_to_cart);

        goodsAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.item_shop_add_to_cart) {
                int goodsId = shopBeanList.get(position).getId();
                viewModel.addToCart(goodsId, 1);
            }
        });

        dataBinding.shopCartFaBtn.setOnClickListener(v -> {
            Intent intent = new Intent(this, CartActivity.class);
            startActivity(intent);
        });
    }

    @Override
    protected Class<ShopViewModel> getViewModel() {
        return ShopViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shop;
    }
}